package com.demo376_摆动序列;

public class Solution {
    public static void main(String[] args) {
        int[] ints = {1,17,5,10,13,15,10,5,16,8};
        int i = wiggleMaxLength(ints);
        System.out.println(i);
    }

    public static int wiggleMaxLength(int[] nums) {
        //思路: 设计两个指针,一前一后, 如果不同则记录


        if (nums.length == 1) {
            return 1;
        }
        int flag = 0;
        if (nums[1] > nums[0]) {
            flag = 1;// 为正为true
        }
        if (nums[1] < nums[0]) {
            flag = -1;
        }
        int max = 0;
        int startIndex = 0;
        for (int i = 2; i < nums.length; i++) {
            // 大于
            if (nums[i] > nums[i - 1]) {
                // 如果上一个是>0
                if (flag == 1) {
                    int len = i - startIndex;
                    max = Math.max(max, len);
                    startIndex = i - 1;
                }else if (flag == -1) {// 如果上一个是<0
                    int len=i-startIndex+1;
                    max = Math.max(max, len);
                    flag = 1;
                }else if (flag == 0) {// 如果等于0
                    int len = i - startIndex;
                    max = Math.max(max, len);
                    startIndex = i - 1;
                }
            }
            // 小于
            if (nums[i] < nums[i - 1]) {
                //如果上一个是大于0
                if (flag == 1) {
                    int len=i-startIndex+1;
                    max = Math.max(max, len);
                    flag = -1;
                } else if (flag == -1) {
                    int len = i - startIndex;
                    max = Math.max(max, len);
                    startIndex = i - 1;
                } else if (flag == 0) {
                    int len = i - startIndex;
                    max = Math.max(max, len);
                    startIndex = i - 1;
                }
            }

            if (nums[i] == nums[i - 1]) {
                int len = i - startIndex;
                startIndex = i;
                max = Math.max(max, len);
            }



        }
        return max;
    }
}
